package com.lemonsun.controller;


import com.lemonsun.pojo.Reply;
import com.lemonsun.pojo.Topic;
import com.lemonsun.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;


@Controller
public class TopicController{
    @Autowired
   private TopicService topicService;


    //查询所有帖子
    @RequestMapping("/topicListService.do")
    public String topicListService(Model model) {
        List<Topic> list = topicService.topicListService();
        model.addAttribute("list", list);
        //返回帖子信息展示页面
        return "homepage";
    }

    //添加帖子
        @RequestMapping("/addTopicInf.do")
    public String addTopicInf(HttpServletRequest request) throws UnsupportedEncodingException {
        Topic topic = new Topic();
            topic.setTitle(new String(request.getParameter("title").getBytes("iso-8859-1"), "utf-8"));
            topic.setAuthor(new String(request.getParameter("author").getBytes("iso-8859-1"), "utf-8"));
            topic.setContent(new String(request.getParameter("context").getBytes("iso-8859-1"), "utf-8"));
            topic.setCretime(new Timestamp(new Date().getTime()));
            topicService.addTopicInf(topic);
            return "addSuccess";
    }

    //查看某个帖子信息
    @RequestMapping("/findAllTopicbyId.do")
    public String findAllTopicbyId(Integer id, Model model) {
        //点击量加一
             topicService.add_click_amount_byId(id);
         //获取帖子信息
        Topic topic = topicService.findAllTopicbyId(id);
        System.out.println("topic:"+topic);
        model.addAttribute("topic", topic);
       //获取评论信息
        List<Topic> replylist = topicService.findAllReplyId(id);
        model.addAttribute("replylist", replylist);
        //传入帖子id
        model.addAttribute("topicId", id);

        //返回客户信息展示页面
        return "showTopic";
    }

    //添加评论
    @RequestMapping("/addReplyInf.do")
    public String addReplyInf(HttpServletRequest request,Model model) throws UnsupportedEncodingException {
        Reply reply = new Reply();
        String id = request.getParameter("topicid");
        reply.setAuthon(new String(request.getParameter("reply_author").getBytes("iso-8859-1"), "utf-8"));
        reply.setContent(new String(request.getParameter("reply_context").getBytes("iso-8859-1"), "utf-8"));
        reply.setT_id(new Integer(request.getParameter("topicid")));
        reply.setCretime(new Timestamp(new Date().getTime()));
        topicService.addReplyInf(reply);

        return "addSuccessReply";
    }







}
